// src/utils/fileUtils.js
export const isSystemFile = (fileName) => {
    // 定义常见的系统文件名或模式
    const systemFilePatterns = [/^desktop\.ini$/i, /^thumbs\.db$/i, /^\.DS_Store$/i];
    return systemFilePatterns.some((pattern) => pattern.test(fileName));
  };

  export const isSystemDirectory = (dirName) => {
    // 定义常见的系统文件夹名或模式
    const systemDirPatterns = [/^Windows$/i, /^System$/i, /^Program Files$/i, /^Applications$/i];
    return systemDirPatterns.some((pattern) => pattern.test(dirName));
  };

  export const traverseDirectory = async (directoryHandle, callback, onError) => {
    try {
      for await (const [name, handle] of directoryHandle.entries()) {
        try {
          if (handle.kind === 'file') {
            if (/\.(jpg|jpeg|png|gif)$/i.test(name)) {
              const file = await handle.getFile();
              callback({
                name: file.name,
                handle,
                file,
                kind: 'file', // 添加 kind 属性
              });
            }
          } else if (handle.kind === 'directory') {
            if (isSystemDirectory(name)) {
              console.warn(`跳过系统文件夹: ${name}`);
              continue;
            }
            callback({
              name,
              handle,
              kind: 'directory', // 添加 kind 属性
            });
            // 如果不需要递归子目录，可以注释掉下面这行
            // await traverseDirectory(handle, callback, onError);
          }
        } catch (innerError) {
          console.warn(`无法访问 ${name}:`, innerError);
          if (onError) onError(innerError);
        }
      }
    } catch (error) {
      console.error('遍历目录时出错:', error);
      if (onError) onError(error);
    }
  };